package com.wyj.connector;

import com.sun.jdi.DoubleType;
import com.wyj.http.HttpServlet;
import com.wyj.http.Request;
import com.wyj.http.Response;
import com.wyj.mapper.MappedContext;
import com.wyj.server.Digester;
import com.wyj.server.Wrapper;
import com.wyj.util.HttpProtocolUtil;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.logging.Logger;

/**
 * TODO: 将endpoint通过socket获取到的内容封装为request
 *
 * @author wuyijie
 * @date 2021/12/14 0:08
 */
public class Processor implements Runnable {
    private static final Logger logger = Logger.getLogger("connector");

    Socket socket;
    // private String headers;
    // private String content;


    public Processor(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            logger.info("[connector starting : " + this + ", current thread : " + Thread.currentThread().getName() + "]");

            Request request = new Request(socket.getInputStream());
            Response response = new Response(socket.getOutputStream());

            String[] split = request.getUrl().split("/");
            //获取应用程序访问的前缀
            String contextPath = split[1];
            System.out.println("contextPath=" + contextPath);
            String url = request.getUrl().substring(contextPath.length() + 1);
            System.out.println("url=" + url);

            Map<String, MappedContext> localhost = Digester.MAPPER.getMappedHostMap().get("localhost").getMappedContextMap();
            if (localhost.containsKey(contextPath)) {
                MappedContext mappedContext = localhost.get(contextPath);
                System.out.println(mappedContext);
                Wrapper wrapper = null;
                if (mappedContext.getContext().getWrapperMap().containsKey(url)) {
                    wrapper = mappedContext.getContext().getWrapperMap().get(url);
                    String servletClass = wrapper.getServletClass();
                    // Thread.currentThread().setContextClassLoader(mappedContext.getClassLoader());
                    System.out.println("mappedContext.getClassLoader() ====>" + mappedContext.getContext().getClassLoader());
                    Class<?> aClass = null;
                    try {
                        aClass = mappedContext.getContext().getClassLoader().loadClass(servletClass);
                        HttpServlet httpServlet = (HttpServlet) aClass.getConstructor().newInstance();
                        httpServlet.service(request, response);
                    } catch (ClassNotFoundException | InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException e) {
                        e.printStackTrace();
                    }
                } else {
                    response.outputHtml("/" + mappedContext.getContext().getDocBase() + url);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

}
